package com.ywwxhz.plugins.eyecare;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

/**
 * Created by ywwxhz on 2014/10/6.
 */
public class EyesCareView extends View implements AnimatorListener {

    private float currentAlpha = 0;
    private float endAlpha;
    private float maxAlpha = 0.7f;
    private AnimatorListener listener;

    public EyesCareView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public EyesCareView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    private Drawable maskDrawable;
    private boolean show = true;

    public EyesCareView(Context context) {
        super(context);
        initView(context, null);
    }

    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    private void initView(Context context, AttributeSet attrs) {
        maskDrawable = new ColorDrawable(Color.BLACK);
        LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        setLayoutParams(mLayoutParams);
        if (Build.VERSION_CODES.JELLY_BEAN > Build.VERSION.SDK_INT) {
            setBackgroundDrawable(maskDrawable);
            maskDrawable.setCallback(mDrawableCallback);
        } else {
            setBackground(maskDrawable);
        }
    }

    @SuppressWarnings("unused")
    private void setCurrentAlpha(float value) {
        currentAlpha = value;
        maskDrawable.setAlpha((int) (currentAlpha * 255));
    }

    private Drawable.Callback mDrawableCallback = new Drawable.Callback() {
        @SuppressWarnings("deprecation")
        @Override
        public void invalidateDrawable(Drawable who) {
            setBackgroundDrawable(who);
        }

        @Override
        public void scheduleDrawable(Drawable who, Runnable what, long when) {
        }

        @Override
        public void unscheduleDrawable(Drawable who, Runnable what) {
        }
    };

    public void show() {
        doAnimate(0, getEndAlpha());
        show = true;
    }

    public void dismiss() {
        doAnimate(currentAlpha, 0);
        show = false;
    }

    private void doAnimate(float start, float end) {
        ValueAnimator colorAnim = ObjectAnimator.ofFloat(this, "currentAlpha",
                start, end);
        colorAnim.setDuration(1500);
        colorAnim.setInterpolator(new AccelerateInterpolator());
        colorAnim.addListener(this);
        colorAnim.start();
    }

    public boolean isShow() {
        return show;
    }

    public float getEndAlpha() {
        return endAlpha;
    }

    public void setEndAlpha(float endAlpha) {
        if (endAlpha <= maxAlpha) {
            this.endAlpha = endAlpha;
        } else {
            this.endAlpha = maxAlpha;
        }
        doAnimate(currentAlpha, this.endAlpha);
    }

    @Override
    public void onAnimationStart(Animator animation) {
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if (listener != null) {
            if (show) {
                listener.onShowEnd();
            } else {
                listener.ondismissEnd();
            }
        }
    }

    @Override
    public void onAnimationCancel(Animator animation) {
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
    }

    public AnimatorListener getAnimatorListener() {
        return listener;
    }

    public void setAnimatorListener(AnimatorListener listener) {
        this.listener = listener;
    }

    public interface AnimatorListener {
        public void onShowEnd();

        public void ondismissEnd();
    }

}
